package game

import (
	"encoding/json"
	"fmt"
	"gosum/server/src/csv"
	"io/ioutil"
	"math/rand"
	"os"
)

type Relics struct {
	RelicsId int
	KeyId    int

	MainEntry  int
	Level      int
	Exp        int
	OtherEntry []int

	RoleId int
	//attribute int
	//roleKey int
}

type ModRelics struct {
	RelicsInfo map[int]*Relics
	MaxKey     int

	player *Player
	path   string
}

func (self *ModRelics) AddItem(itemId int, num int64) {
	config := csv.GetRelicsConfig(itemId)
	//relicsConfig := csv.GetItemConfig(itemId)
	if config == nil {
		fmt.Println("圣遗物不存在")
		return
	}

	if len(self.RelicsInfo)+int(num) > csv.RELICS_MAX_COUNT {
		fmt.Println("超出最大值")
		return
	}

	for i := int64(0); i < num; i++ {
		relics := self.NewRelics(itemId)
		self.RelicsInfo[relics.KeyId] = relics
		fmt.Println("获得圣遗物:")
		//fmt.Println("获得圣遗物: ", relicsConfig.ItemName, "圣遗物编号: ", relics.KeyId)
		relics.ShowInfo()
	}
}

func (self *ModRelics) NewRelics(itemId int) *Relics {
	relicsRel := new(Relics)
	relicsRel.RelicsId = itemId
	self.MaxKey++
	relicsRel.KeyId = self.MaxKey
	config := csv.ConfigRelicsMap[itemId]
	if config == nil {
		return nil
	}
	relicsRel.MainEntry = self.MakeMainEntry(config.MainGroup)
	for i := 0; i < config.OtherGroupNum; i++ {
		if i == config.OtherGroupNum-1 {
			randNum := rand.Intn(csv.PERCENT_ALL)
			if randNum < csv.ALL_ENTRY_RATE {
				relicsRel.OtherEntry = append(relicsRel.OtherEntry, self.MakeOtherEntry(relicsRel, config.OtherGroup))
			}
		} else {
			relicsRel.OtherEntry = append(relicsRel.OtherEntry, self.MakeOtherEntry(relicsRel, config.OtherGroup))
		}
	}
	return relicsRel
}

func (self *ModRelics) MakeMainEntry(MainGroup int) int {
	configs, ok := csv.ConfigRelicsEntryGroupMap[MainGroup]
	if !ok {
		return 0
	}
	allRate := 0
	for _, v := range configs {
		allRate += v.Weight
	}
	randNum := rand.Intn(allRate)
	nowNum := 0
	for _, v := range configs {
		nowNum += v.Weight
		if nowNum > randNum {
			return v.Id
		}
	}
	return 0
}

func (self *ModRelics) MakeOtherEntry(relics *Relics, OtherGroup int) int {
	configs, ok := csv.ConfigRelicsEntryGroupMap[OtherGroup]
	if !ok {
		return 0
	}
	configNow := csv.GetRelicsConfig(relics.RelicsId)
	if configNow == nil {
		return 0
	}
	if len(relics.OtherEntry) >= configNow.OtherGroupNum {
		//词条排重
		allEntry := make(map[int]int)

		for _, id := range relics.OtherEntry {
			otherConfig, _ := csv.ConfigRelicsEntryMap[id]
			if otherConfig != nil {
				allEntry[otherConfig.AttrType] = csv.LOGIC_TRUE
			}
		}

		allRate := 0
		for _, v := range configs {
			_, ok := allEntry[v.AttrType]
			if !ok {
				continue
			}
			allRate += v.Weight
		}
		randNum := rand.Intn(allRate)
		nowNum := 0
		for _, v := range configs {
			_, ok := allEntry[v.AttrType]
			if !ok {
				continue
			}
			nowNum += v.Weight
			if nowNum > randNum {
				return v.Id
			}
		}
	} else {
		//词条排重
		allEntry := make(map[int]int)
		mainConfig, _ := csv.ConfigRelicsEntryMap[relics.MainEntry]
		if mainConfig != nil {
			allEntry[mainConfig.AttrType] = csv.LOGIC_TRUE
		}
		for _, id := range relics.OtherEntry {
			otherConfig, _ := csv.ConfigRelicsEntryMap[id]
			if otherConfig != nil {
				allEntry[otherConfig.AttrType] = csv.LOGIC_TRUE
			}
		}

		allRate := 0
		for _, v := range configs {
			_, ok := allEntry[v.AttrType]
			if ok {
				continue
			}
			allRate += v.Weight
		}

		randNum := rand.Intn(allRate)
		nowNum := 0
		for _, v := range configs {
			_, ok := allEntry[v.AttrType]
			if ok {
				continue
			}
			nowNum += v.Weight
			if nowNum > randNum {
				return v.Id
			}
		}
	}
	return 0
}

func (self *Relics) ShowInfo() {
	fmt.Println(fmt.Sprintf("key:%d,Id:%d", self.KeyId, self.RelicsId))
	fmt.Println(fmt.Sprintf("当前等级:%d,当前经验:%d", self.Level, self.Exp))
	//mainEntryConfig := csv.ConfigRelicsEntryMap[self.MainEntry]
	mainEntryConfig := csv.GetRelicsLevelConfig(self.MainEntry, self.Level)
	if mainEntryConfig != nil {
		fmt.Println(fmt.Sprintf("主词条属性:%s,值:%d", mainEntryConfig.AttrName, mainEntryConfig.AttrValue))
	}
	for _, v := range self.OtherEntry {
		otherEntryConfig := csv.ConfigRelicsEntryMap[v]
		if otherEntryConfig != nil {
			fmt.Println(fmt.Sprintf("副词条属性:%s,值:%d", otherEntryConfig.AttrName, otherEntryConfig.AttrValue))
		}
	}

}

func (self *ModRelics) UpRelicsLevel(itemId int, player *Player) {
	relics := self.RelicsInfo[itemId]
	if relics == nil {
		fmt.Println("找不到对应圣遗物")
		return
	}
	relics.Exp += 100000
	for {
		nextLevelConfig := csv.GetRelicsLevelConfig(relics.MainEntry, relics.Level+1)
		if nextLevelConfig == nil {
			break
		}
		if relics.Exp < nextLevelConfig.NeedExp {
			break
		}
		relics.Level += 1
		relics.Exp -= nextLevelConfig.NeedExp

		if relics.Level%4 == 0 {
			relicsConfig := csv.ConfigRelicsMap[relics.RelicsId]
			if relicsConfig != nil {
				relics.OtherEntry = append(relics.OtherEntry, self.MakeOtherEntry(relics, relicsConfig.OtherGroup))
			}
		}
	}
	relics.ShowInfo()
}

func (self *ModRelics) RelicsTop(player *Player) {
	relics := self.NewRelics(7000005)
	relics.Level = 20
	config := csv.GetRelicsConfig(relics.RelicsId)
	if config == nil {
		return
	}
	for i := 0; i < 5; i++ {
		relics.OtherEntry = append(relics.OtherEntry, self.MakeOtherEntry(relics, config.OtherGroup))
	}
	relics.ShowInfo()
}

//测试双爆头
func (self *ModRelics) RelicsTestBest(player *Player) {
	config := csv.GetRelicsConfig(7000005)
	if config == nil {
		return
	}
	testTimes := 500000
	relicsBestInfo := make([]*Relics, 0)

	for i := 0; i < testTimes; i++ {
		relics := self.NewRelics(7000005)
		relics.Level = 20
		config := csv.GetRelicsConfig(relics.RelicsId)
		if config == nil {
			return
		}
		for i := 0; i < 5; i++ {
			relics.OtherEntry = append(relics.OtherEntry, self.MakeOtherEntry(relics, config.OtherGroup))
		}

		configMain := csv.ConfigRelicsEntryMap[relics.MainEntry]
		if configMain == nil {
			continue
		}
		if configMain.AttrType != 4 && configMain.AttrType != 5 {
			continue
		}
		bestRelicsCount := 0
		for _, v := range relics.OtherEntry {
			configOther := csv.ConfigRelicsEntryMap[v]
			if configOther == nil {
				continue
			}
			if configOther.AttrType != 4 && configOther.AttrType != 5 {
				continue
			}
			bestRelicsCount++
		}
		if bestRelicsCount < 6 {
			continue
		}
		relicsBestInfo = append(relicsBestInfo, relics)
	}
	fmt.Println(fmt.Sprintf("生成了圣遗物头部位%d个，极品数量%d", testTimes, len(relicsBestInfo)))

	for _, v := range relicsBestInfo {
		v.ShowInfo()
	}
}

func (self *ModRelics) SaveData() {
	content, err := json.Marshal(self)
	if err != nil {
		return
	}
	err = ioutil.WriteFile(self.path, content, os.ModePerm)
	if err != nil {
		return
	}
}

func (self *ModRelics) LoadData(player *Player) {

	self.player = player
	self.path = self.player.localPath + "/relics.json"

	configFile, err := ioutil.ReadFile(self.path)
	if err != nil {
		fmt.Println("error")
		return
	}
	err = json.Unmarshal(configFile, &self)
	if err != nil {
		self.InitData()
		return
	}

	if self.RelicsInfo == nil {
		self.RelicsInfo = make(map[int]*Relics)
	}
	return
}

func (self *ModRelics) InitData() {

}
